package com.budwk.app.util;

/**
 * @desc: 方法描述
 * @Author: laoschiang
 * @Date: 2023/11/14 8:48
 * @info:
 */

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.budwk.app.sqfw.constants.CommonConstants;
import lombok.extern.slf4j.Slf4j;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
@Slf4j
public class DateUtils {
    public static String formateDate(long time)
    {
        DateFormat serverDateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z",
                Locale.ENGLISH);
        serverDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
        return serverDateFormat.format(time);
    }

    public static Date toDateByLocalDateTime(LocalDateTime localDateTime) {
        return Date.from(localDateTime.atZone(ZoneOffset.ofHours(8)).toInstant());
    }

    /**
     * 获取当前时间的最后一天
     * @param date 时间
     * @return 返回字符串
     */
    public static String getCurrentDataLastDay(Date date) {
        String fillDate = DateUtil.format(date, DatePattern.NORM_DATE_PATTERN);
        LocalDate parseDate = LocalDateTimeUtil.parseDate(fillDate, DatePattern.NORM_DATE_PATTERN);
        String dateStr = String.format("%s%s", parseDate.with(TemporalAdjusters.lastDayOfMonth()), CommonConstants.LAST_TIME);
        log.info("getLastMonthDay res: {}", dateStr);
        return dateStr;

    }
    /**
     * 获取当前时间的最后一天
     * @param date 时间
     * @return 返回字符串
     */
    public static String getCurrentDataLastDayStr(String date) {
        LocalDate parseDate = LocalDateTimeUtil.parseDate(date, DatePattern.NORM_DATE_PATTERN);
        String lastDay = String.format("%s%s", parseDate.with(TemporalAdjusters.lastDayOfMonth()), CommonConstants.LAST_TIME);
        return lastDay;
    }
}
